
do "...\First.do"

**********************************************************************************
* Sum stat on the physician level
**********************************************************************************
clear 
use "$work\Yder_9518_long.dta"

keep if inrange(year,1999,2016)

merge m:1 ydernr year using "$work\gp_closures_yearly.dta"
drop if _merge==2
drop _merge

g closing=(year_max==year)
bys pnr: egen max_closing=max(closing)

replace age=. if age<35
g retire=(age>=60)
bys ydernr: egen max_retire=max(retire)

bys pnr: egen mean=mean(solo)
drop solo 
rename mean solo

bys pnr: egen max=max(dk)
drop dk 
rename max dk

drop if year_doctor>year

merge m:1 ydernr year using "$work\N_patients.dta"
keep if _merge==3
drop _merge

sum N_patients, d
drop if N_patients<750
drop if N_patients>9000

********************************************************************************

keep pnr ses_parent male yob dk gp_school mis_ses_parent ses_parent solo year_doctor  max_closing  mor_udd_level far_udd_level
duplicates drop

g ku=(gp_school==1)
g au=(gp_school==2)
g sdu=(gp_school==3)
g other=(gp_school==4)
drop if year_doctor==.

g non_missing_ses=(mis_ses_parent==0)
tab ses_parent
replace ses_parent=. if mis_ses_parent==1

********************************************************************************

label var ku "University of Copenhagen"
label var au "Aarhus University"
label var sdu "University of Southern Denmark"
label var other "Other University"
lab var male "Male"
lab var dk "Ethnic Danish"
lab var yob "Year of birth"
lab var year_doctor "year of graduation"
lab var solo "Solo practise"
lab var ses_parent "Low SES"
lab var mis_ses_parent "Missing SES"
lab var non_missing_ses "Non missing SES"


********************************************************************************
file open myfile using "$table\Table1_PanelB.tex", replace write
	file write myfile " & All Physicians & Non-closing sample & Closing sample & High SES & Low SES  \\" _n

	
	
estimates clear	
cap drop i
g i=0
foreach var of varlist male_doctor yob dk ses_parent non_missing_ses ku sdu au other {
	
	local l`var': variable label `var'

replace i=i+1

	qui: sum `var'
	local a`i': disp %8.3f r(mean)

	qui: sum `var' if max_closing==0
	local b`i': disp %8.3f r(mean)

	qui: sum `var' if max_closing==1
	local c`i': disp %8.3f r(mean)

	qui: sum `var' if ses_parent==0 & mis_ses_parent==0 & max_closing==0
	local d`i': disp %8.3f r(mean)

	qui: sum `var' if ses_parent==1 & mis_ses_parent==0 & max_closing==0
	local e`i': disp %8.3f r(mean)

	
file write myfile " `l`var'' & `a`i'' & `b`i'' & `c`i'' & `d`i'' & `e`i'' \\" _n
	
	
}

sum male_doctor 
local i1: disp %11.0gc r(N)
sum male_doctor if max_closing==0
local i2: disp %11.0gc r(N)
sum male_doctor if max_closing==1
local i3: disp %11.0gc r(N)
sum male_doctor if ses_parent==0 & mis_ses_parent==0 & max_closing==0
local i4: disp %11.0gc r(N)
sum male_doctor if ses_parent==1 & mis_ses_parent==0 & max_closing==0
local i5: disp %11.0gc r(N)

file write myfile "Number of observations & `i1' & `i2' & `i3' & `i4' & `i5' \\" 
file close myfile


**********************************************************************************
* Sum stat on the clinic level
**********************************************************************************

clear
use  "$work\gp_ses"

g non_missing_ses=(yder_ses_missing!=1)

merge 1:1 ydernr year using "$work\N_patients.dta"
keep if _merge==3
drop _merge

sum N_patients, d
drop if N_patients<750
drop if N_patients>9000

collapse (mean) N_doctors yder_ses_max yder_ses_min yder_ses_mean solo non_missing_ses, by(ydernr)

merge m:1 ydernr using "$work\gp_closures_year.dta"
drop _merge

g closing=(year_max<2017)
replace closing=0 if year_max==.



********************************************************************************
label var solo "Solo"
label var N_doctors "Number of doctors in clinic"
label var yder_ses_max "Low SES"
label var non_missing_ses "Not-missing SES"

********************************************************************************

file open myfile using "$table\Table1_PanelC.tex", replace write
file write myfile " & All clinics & Non-closing clinics & Closing clinics & High SES clinics & Low SES clinics \\" _n
	
estimates clear	
cap drop i
g i=0
foreach var of varlist solo N_doctors non_missing_ses yder_ses_max {
	
	local l`var': variable label `var'

replace i=i+1

	qui: sum `var'
	local a`i': disp %8.3f r(mean)

	qui: sum `var' if closing==0
	local b`i': disp %8.3f r(mean)

	qui: sum `var' if closing==1
	local c`i': disp %8.3f r(mean)

	qui: sum `var' if closing==0 & yder_ses_max==0 & non_missing_ses==1
	local d`i': disp %8.3f r(mean)

	qui: sum `var' if closing==0 & yder_ses_max==1 & non_missing_ses==1
	local e`i': disp %8.3f r(mean)
	
file write myfile " `l`var'' & `a`i'' & `b`i'' & `c`i'' & `d`i'' & `e`i'' \\" _n
	
	
}



sum solo 
local i1: disp %11.0gc r(N)
sum solo if closing==0
local i2: disp %11.0gc r(N)
sum solo if closing==1
local i3: disp %11.0gc r(N)
sum solo if closing==0 & yder_ses_max==0 & non_missing_ses==1
local i4: disp %11.0gc r(N)
sum solo if closing==0 & yder_ses_max==1 & non_missing_ses==1
local i5: disp %11.0gc r(N)


file write myfile "Number of observations & `i1' & `i2' & `i3' & `i4' & `i5'   \\" 
file close myfile

